package com.deyuanyun.pic.pipe.support.utils;

import com.deyuanyun.pic.common.cache.TempStoreDataSupport;
import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.domain.pipe.PipeLine;
import com.deyuanyun.pic.domain.pipe.PipeSegment;
import com.deyuanyun.pic.domain.pipe.ValveRoom;
import com.deyuanyun.pic.service.pipe.PipeLineService;
import com.deyuanyun.pic.service.pipe.PipeSegmentService;
import com.deyuanyun.pic.service.pipe.ValveRoomService;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ListMultimap;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xu on 2015/12/31.
 * pipe需要用到的字典工具
 */
public class PipeDictUtils {
    static Logger logger=Logger.getLogger(PipeDictUtils.class);
    private static HashBiMap<String,String> pipeLineBiMap ;//管道map  id-name
    private static HashBiMap<String,String> pipeSegmentBiMap ;//管段map
    private static HashBiMap<String,String> valveRoomBiMap ;//场站阀室

    private static Map<String,PipeLine> pipeLineMap;//管道 id-object
    private static ListMultimap<String,PipeSegment> pipeSegmentMultimap;//管段 parentid-object

    public static synchronized void inidData(boolean isForceRefresh){

        pipeLineBiMap = TempStoreDataSupport.pullData("PipeDictUtils_pipeLineBiMap");//管道
        pipeSegmentBiMap = TempStoreDataSupport.pullData("PipeDictUtils_pipeSegmentBiMap");
        pipeLineMap = TempStoreDataSupport.pullData("PipeDictUtils_pipeLineMap");//管道
        pipeSegmentMultimap = TempStoreDataSupport.pullData("PipeDictUtils_pipeSegmentMultimap");//管段
        valveRoomBiMap = TempStoreDataSupport.pullData("PipeDictUtils_valveRoomBiMap");//场站阀室

        if (ObjectUtil.isEmptyAll(pipeLineMap)
                ||ObjectUtil.isEmptyAll(pipeLineBiMap)
                ||ObjectUtil.isEmptyAll(pipeSegmentBiMap)
                ||ObjectUtil.isEmptyAll(pipeSegmentMultimap)
                ||ObjectUtil.isEmptyAll(valveRoomBiMap)
                || isForceRefresh){
            PipeSegmentService segmentService= SpringAppliction.getBean(PipeSegmentService.class);
            PipeLineService lineService=SpringAppliction.getBean(PipeLineService.class);
            ValveRoomService roomService=SpringAppliction.getBean(ValveRoomService.class);

            List<PipeLine> pipeLineList=lineService.getLine();
            List<PipeSegment> segmentList=segmentService.getAllSegment(new HashMap());
            List<ValveRoom> roomList= roomService.queryAllValueRoomList(new HashMap());
            TempStoreDataSupport.pushDataByTimeMinute("PipeDICT_getRooms",roomList,30);

            pipeLineBiMap= HashBiMap.create();
            pipeSegmentBiMap=HashBiMap.create();
            pipeLineMap=new HashMap<>();
            pipeSegmentMultimap= ArrayListMultimap.create();
            valveRoomBiMap = HashBiMap.create();

            for (ValveRoom valveRoom:roomList){
                valveRoomBiMap.put(valveRoom.getSiteName(),valveRoom.getId());
            }
            TempStoreDataSupport.pushDataByTimeMinute("PipeDictUtils_valveRoomBiMap",valveRoomBiMap,30);

            for (PipeLine pipeLine:pipeLineList){
                pipeLineBiMap.put(pipeLine.getName(),pipeLine.getId());
                pipeLineMap.put(pipeLine.getId(),pipeLine);
            }
            TempStoreDataSupport.pushDataByTimeMinute("PipeDictUtils_pipeLineBiMap",pipeLineBiMap,30);
            TempStoreDataSupport.pushDataByTimeMinute("PipeDictUtils_pipeLineMap",pipeLineMap,30);

            for (PipeSegment segment:segmentList){
                pipeSegmentBiMap.put(pipeLineBiMap.inverse().get(segment.getPipeLineID())+"__"+segment.getName(),segment.getId());
                pipeSegmentMultimap.put(segment.getPipeLineID(),segment);
            }
            TempStoreDataSupport.pushDataByTimeMinute("PipeDictUtils_pipeSegmentBiMap",pipeSegmentBiMap,30);
            TempStoreDataSupport.pushDataByTimeMinute("PipeDictUtils_pipeSegmentMultimap",pipeSegmentMultimap,30);

        }
    }

    /**
     * 获取场站阀室列表
     * @return
     */
    public static List<ValveRoom> getRooms(boolean foceFresh){
        String key="PipeDICT_getRooms";
        List<ValveRoom> roomList=TempStoreDataSupport.pullData(key);
        if (ObjectUtil.isEmptyAll(roomList)||foceFresh){
            ValveRoomService roomService=SpringAppliction.getBean(ValveRoomService.class);
            roomList= roomService.queryAllValueRoomList(new HashMap());
            TempStoreDataSupport.pushDataByTimeMinute(key,roomList,30);
            return roomList;
        }
        else {
            return roomList;
        }

    }

    /**
     * 查询场站阀室名称 根据id
     * @param id
     * @return
     */
    public static String getRoomNameById(String id){
        inidData(false);
        return valveRoomBiMap.inverse().get(id);

    }
    /**
     * 查询场站阀室名称 根据name
     * @param name
     * @return
     */
    public static String getRoomIByName(String name){
        inidData(false);
        return valveRoomBiMap.get(name);
    }

    public static Map<String,String> getAllLine(){
        inidData(false);
        BiMap hashBiMap= pipeLineBiMap.inverse();
        return hashBiMap;
    }


    /**
     * 根据name获取管段id
     * @param name
     * @return
     */
    public static String getLineIdByName(String name){
        inidData(false);
        return pipeLineBiMap.get(name);
    }

    /**
     * 根据管道和管段名称获取管段id
     * @param lineName
     * @param segmentName
     * @return
     */
    public static String getSegmentByName(String lineName,String segmentName){
        inidData(false);
        return pipeSegmentBiMap.get(lineName+"__"+segmentName);
    }

    /**
     * 根据lineid获取管道
     * @param lineId
     * @return
     */
    public static PipeLine getLineById(String lineId){
        inidData(false);
        return pipeLineMap.get(lineId);
    }

    /**
     * 根据管道id获取管道
     * @param segmentId
     * @return
     */
    public static PipeSegment getSegment(String segmentId){
        inidData(false);
        List<PipeSegment> segments =new ArrayList<>(pipeSegmentMultimap.values()) ;
        for (PipeSegment segment:segments){
            if (segmentId.equals(segment.getId())){
                return segment;
            }
        }
        return null;
    }

    /**
     * 根据管道id获取管段
     * @param lineId
     * @return
     */
    public static List<PipeSegment> getSegmentByLineId(String lineId){
        inidData(false);
        return pipeSegmentMultimap.get(lineId);
    }




}
